package com.beyondsoft.giinii.brie.jni;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView;

public class ListResult extends ListActivity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Use an existing ListAdapter that will map an array
        // of strings to TextViews
        
        Intent intent = getIntent();
        filelist = intent.getStringArrayListExtra("fileList");
        clslist = intent.getIntegerArrayListExtra("clsList");
        uniqueclslist = new ArrayList<String>();
        
        Set<Integer> tmpSet = new HashSet<Integer>();
        tmpSet.addAll(clslist);
        Iterator<Integer> it = tmpSet.iterator();
        while(it.hasNext())
        {
        	uniqueclslist.add(String.valueOf(it.next()));
        }
        java.util.Collections.sort(uniqueclslist);
        
//        setListAdapter(new ArrayAdapter<String>(this,
//                android.R.layout.simple_list_item_1, uniqueclslist));
        setListAdapter(new EfficientAdapter(this, uniqueclslist, filelist, clslist));
        
        final ListView listView = getListView();
        listView.setOnItemClickListener(ShowImages);
    }
    
    private static class EfficientAdapter extends BaseAdapter 
    {
        private LayoutInflater mInflater;
        ArrayList<String> mUniList;
        ArrayList<String> mFileList;
        ArrayList<Integer> mClsList;
        public EfficientAdapter(Context context, ArrayList<String> uniqueclslist, 
        		ArrayList<String> filelist, ArrayList<Integer> clslist ) 
        {
            // Cache the LayoutInflate to avoid asking for a new one each time.
            mInflater = LayoutInflater.from(context);
            mUniList = uniqueclslist;
            mFileList = filelist;
            mClsList = clslist;
        }
		public int getCount() {
			return mUniList.size();
		}

		public Object getItem(int position) {
			return position;
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.list_item_icon_text, null);

                // Creates a ViewHolder and store references to the two children views
                // we want to bind data to.
                holder = new ViewHolder();
                holder.text = (TextView) convertView.findViewById(R.id.text);
                holder.icon = (ImageView) convertView.findViewById(R.id.icon);

                convertView.setTag(holder);
            } else {
                // Get the ViewHolder back to get fast access to the TextView
                // and the ImageView.
                holder = (ViewHolder) convertView.getTag();
            }

            // Bind the data efficiently with the holder.
			ArrayList<String> flist = new ArrayList<String>();
			for(int i=0; i<mClsList.size(); ++i)
			{
				if(mClsList.get(i).toString() == mUniList.get(position))
				{
					flist.add(mFileList.get(i));
				}
			}
            
            holder.text.setText("  Class " + position + ", " + flist.size() + "Pics in this class");
            holder.icon.setImageBitmap(BitmapFactory.decodeFile(flist.get(0)));

            return convertView;
		}
        static class ViewHolder {
            TextView text;
            ImageView icon;
        }
    }
    
    private OnItemClickListener ShowImages = new OnItemClickListener() 
    {
		public void onItemClick(AdapterView<?> parent, View view, int position,	long id)        
		{
			ArrayList<String> flist = new ArrayList<String>();
			for(int i=0; i<clslist.size(); ++i)
			{
				if(clslist.get(i).toString() == uniqueclslist.get(position))
				{
					flist.add(filelist.get(i));
				}
			}
    		Intent iten = new Intent(ListResult.this, ShowImages.class);
    		//iten.putExtra("FileName", resultFile.getAbsolutePath() + "/" + resultFile.list()[position]);
    		iten.putExtra("filelist", flist);
    		startActivity(iten);
        }
    };
    
    private ArrayList<String> filelist = null;
    private ArrayList<Integer> clslist = null;
    private ArrayList<String> uniqueclslist = new ArrayList<String>();

}
